package day9;
import java.io.*;

public class Tihuan {
    public static void main(String[] args) {
        //原有内容
        String a="金正恩";
//        替换后的内容
        String b="金三胖";
//        读
        File file = new File("D:\\Documents\\桌面\\chaoxian.txt");
        try {
            FileReader fileReader = new FileReader(file);
            BufferedReader c = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
//            内存流作为临时流
            CharArrayWriter d = new CharArrayWriter();
//            替换
            String line=null;
            while ((line=c.readLine())!=null){

//                替换每行中符合条件的字符串
                line=line.replaceAll(a,b);
//                将该行写入内存
                d.write(line);
//                添加换行符
                d.append("\r\n");
            }
//            关闭输入流
            c.close();
//           将内存中的流写入文件
            FileWriter fileWriter = new FileWriter(file);
            d.writeTo(fileWriter);
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
